package org.delphy.indy2.containers
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextLineMetrics;
	
	import mx.containers.HBox;
	import mx.controls.Label;
	import mx.core.Application;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.events.FlexMouseEvent;
	import mx.managers.PopUpManager;
	import mx.styles.StyleManager;
	
	import org.delphy.indy2.containers.CarbonPanel;
	import org.delphy.indy2.core.ICarbonComponent;
	import org.delphy.indy2.containers.carbonMagnifyWindowClasses.*;
	import org.delphy.indy2.layers.*;
	
	//----------------------------------
	//  Styles
	//----------------------------------	
	
	[Style(name="textFilter", type="String", inherit="no")]
	
	public class CarbonMagnifyWindow extends CarbonPanel implements ICarbonComponent
	{
		/**
		 * @public
		 */
		[Bindable]
		public var text:String;
		
		/**
		 * @protected
		 */
		protected var notifyText:Label;
		
		/**
		 * @protected
		 */
		protected var layoutBox:HBox;
		
		/**
		 * Constructor
		 */
		public function CarbonMagnifyWindow()
		{
			super();
			
			// Disable any scrollbars here
			resizable=false;
			horizontalScrollPolicy="off";
			verticalScrollPolicy="off";
		}
		
		/**
		 * @public
		 */
		public function popUp():void{
			// Remove self from parent, if there are any.
			// This is to avoid causing exceptions
			if(parent)
				parent.removeChild(this);
			
			PopUpManager.addPopUp(this,DisplayObject(Application.application),true);
			PopUpManager.centerPopUp(this);
			
			addEventListener(MouseEvent.CLICK,onClick);
			addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE,onClick);
		}
		
		/**
		 * @protected
		 * Event handler for MouseEvent.CLICK
		 */
		protected function onClick(evt:Event):void{
			PopUpManager.removePopUp(this);
		}
		
		/**
		 * @protected
		 */
		override protected function createChildren():void{
			styleName="carbonMagnifyWindow";
			
			super.createChildren();
			
			// Create a all-centered box
			layoutBox=new HBox();
			layoutBox.setStyle("horizontalAlign","center");
			layoutBox.setStyle("verticalAlign","middle");
			layoutBox.horizontalScrollPolicy="off";
			layoutBox.verticalScrollPolicy="off";
			
			addChild(layoutBox);
			
			notifyText=new Label();
			
			// Pass style to notifyText
			if(getStyle("color"))
				notifyText.setStyle("color", getStyle("color"));
			else
				notifyText.setStyle("color",StyleManager.getStyleDeclaration(".carbonMagnifyWindow").getStyle("color"));

			if(getStyle("fontFamily"))
				notifyText.setStyle("fontFamily", getStyle("fontFamily"));
			else
				notifyText.setStyle("fontFamily",StyleManager.getStyleDeclaration(".carbonMagnifyWindow").getStyle("fontFamily"));
			
			if(getStyle("fontStyle"))
				notifyText.setStyle("fontStyle", getStyle("fontStyle"));
			else
				notifyText.setStyle("fontStyle",StyleManager.getStyleDeclaration(".carbonMagnifyWindow").getStyle("fontStyle"));

			if(getStyle("fontSize"))
				notifyText.setStyle("fontSize", getStyle("fontSize"));
			else
				notifyText.setStyle("fontSize",StyleManager.getStyleDeclaration(".carbonMagnifyWindow").getStyle("fontSize"));

			layoutBox.addChild(notifyText);
			
			// Override content and border properties
			Container(_content).horizontalScrollPolicy="off";
			Container(_content).verticalScrollPolicy="off";	
			
			Container(_content).setStyle("topLeftRadius",_border.topLeftRadius);
			Container(_content).setStyle("topRightRadius",_border.topRightRadius);
			Container(_content).setStyle("bottomLeftRadius",_border.bottomLeftRadius);
			Container(_content).setStyle("bottomRightRadius",_border.bottomRightRadius);
		}
		
		/**
		 * @protected
		 */
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			notifyText.text=text;
			notifyText.filters=FilterFactory.createFilters(this, "textFilter");
			
			// Measure the geometry by the text size
			var metric:TextLineMetrics=notifyText.getLineMetrics(0);
			
			if(metric.width>=stage.stageWidth){
				width=stage.stageWidth-100;
			}
			else{
				width=metric.width+200;
			}
			
			if(metric.height>= stage.stageHeight){
				height=stage.stageHeight-30;
			}			
			else{
				height=metric.height+30;
			}		
			
			layoutBox.width=width;
			layoutBox.height=height;
		}
		
		/**
		 * @public
		 */
		override public function get defaultStyleName():String{
			return "carbonMagnifyWindow";
		}
	}
}